package view.impl;

import java.io.FileNotFoundException;

import model.EMenuState;
import model.ModelFactory;
import model.impl.menu.IMainMenu;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;

import util.ResLoc;
import view.impl.menu.SingleLayeredMenu;
import view.impl.menu.items.IMenuItem;
import view.impl.menu.items.MenuButton;


/**
 * A class that implements the interface IViewState. Defines a state that displays
 * the main menu with three options; Play, Settings and Exit.
 * 
 * @author Julian Lindblad
 */
class MainMenuState implements IViewState {
	
	//private static final MainMenuState INSTANCE = new MainMenuState();
	
	private int windowWidth = 800;
	private int windowHeight = 600;
	
	private Image background;
	private UnicodeFont uFont = null;
	
	private SingleLayeredMenu menu = new SingleLayeredMenu();
	
	/**
	 * Private constructor, as defined in Singleton-pattern.
	 */
	MainMenuState() throws RenderException {
		
		try {
			uFont = FontContainer.getMenuFont();
		} catch (SlickException e) {
			throw new RenderException("Unable to load font in main menu.", e);
		} catch (FileNotFoundException e) {
			throw new RenderException("Unable to load font in main menu.", e);
		}
		
		// Initiate background rendering.
		try {
			background = new Image(ResLoc.getResource("MainMenu.background"));
		} catch (SlickException e) {
			throw new RenderException("Unable to load background for the main menu.", e);
		} catch (FileNotFoundException e) {
			throw new RenderException("Unable to load background for the main menu.", e);
		}
		
		// Initiate menu.
		initMenu();
	}
	
	/**
	 * Initiates the menu-instance variable so it can be drawn.
	 */
	private void initMenu() {
		
		IMenuItem playButton = new MenuButton("Play", 
												uFont, 
												(windowWidth - uFont.getWidth("Play")) / 2, 
												windowHeight / 2);
		
		IMenuItem settingsButton = new MenuButton("Settings", 
											  uFont, 
											  (windowWidth - uFont.getWidth("Settings")) / 2, 
											  (int) ((float)windowHeight / 2f + (float)uFont.getLineHeight() * 1.5));
		
		IMenuItem exitButton = new MenuButton("Exit", 
												  uFont, 
												  (windowWidth - uFont.getWidth("Exit")) / 2, 
												  windowHeight / 2 + uFont.getLineHeight() * 3);
		
		menu.addMenuItem(playButton, 0);
		menu.addMenuItem(settingsButton, 1);
		menu.addMenuItem(exitButton, 2);
		
		
	}

	/* (non-Javadoc)
	 * @see client.view.IViewState#changeResolution(int, int)
	 */
	@Override
	public void render(Graphics g) {
		
		// Draw background.
		background.draw(0, 0, windowWidth, windowHeight);
		
		IMainMenu model = ModelFactory.getViewModel().getMenuState();
		menu.draw(model.getMarkedAlternative(), g, windowWidth, windowHeight);
	}
	
	/* (non-Javadoc)
	 * @see client.view.IViewState#render(client.view.RenderParameters)
	 */
	@Override
	public void changeResolution(int targetWidth, int targetHeight) {
		windowWidth = targetWidth;
		windowHeight = targetHeight;
	}
	
	@Override
	public EMenuState getEMenuTyoe() {
		return EMenuState.MAIN_MENU;
	}
}
